<!-- 车队编辑弹窗 -->
<template>
  <ele-modal
    form
    :width="640"
    :model-value="modelValue"
    :title="isUpdate ? '修改车队' : '新建车队'"
    @update:modelValue="updateModelValue"
  >
    <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="车队名称" prop="title">
        <el-input v-model="form.title" />
      </el-form-item>
      <el-form-item label="所属区域" prop="area_id">
        <el-select clearable v-model="form.area_id" placeholder="请选区域">
          <el-option
            v-for="(item, index) in areaList"
            :key="item.id"
            :label="item.title"
            :value="item.id"
            @click="getAreaId(index)"
          >
            {{ item.title }}
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="updateModelValue(false)">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save">
        保存
      </el-button>
    </template>
  </ele-modal>
</template>

<script setup>
  import { ref, reactive, watch } from 'vue';
  import { EleMessage } from 'ele-admin-plus/es';
  import { useFormData } from '@/utils/use-form-data';
  import { updateCompany, pageMyCompanyArea } from '@/api/company';
  const emit = defineEmits(['done', 'update:modelValue']);

  const props = defineProps({
    /** 弹窗是否打开 */
    modelValue: Boolean,
    /** 修改回显的数据 */
    data: Object
  });

  /** 是否是修改 */
  const isUpdate = ref(false);

  /** 提交状态 */
  const loading = ref(false);

  /** 表单实例 */
  const formRef = ref(null);

  /** 表单数据 */
  const [form, resetFields, assignFields] = useFormData({
    id: 0,
    title: '',
    area: '',
    area_id: ''
  });

  /** 表单验证规则 */
  const rules = reactive({
    title: [
      {
        required: true,
        message: '请输入车队名称',
        type: 'string',
        trigger: 'blur'
      }
    ],
    area_id: [
      {
        required: true,
        message: '请选择所属区域',
        type: 'number',
        trigger: 'blur'
      }
    ]
  });

  /** 保存编辑 */
  const save = () => {
    console.log(form);
    formRef.value?.validate?.((valid) => {
      if (!valid) {
        return;
      }
      loading.value = true;
      updateCompany(form)
        .then((msg) => {
          loading.value = false;
          EleMessage.success(msg);
          updateModelValue(false);
          emit('done');
        })
        .catch((e) => {
          loading.value = false;
          EleMessage.error(e.message);
        });
    });
  };

  /** 区域列表 */
  const areaList = ref([]);
  const getArea = () => {
    pageMyCompanyArea({ limit: 99999999 })
      .then((res) => {
        res.list.shift();
        areaList.value = res.list;
      })
      .catch((e) => {
        loading.value = false;
        EleMessage.error(e.message);
      });
  };
  const getAreaId = (e) => {
    form.area = areaList.value[e].title;
  };

  /** 更新modelValue */
  const updateModelValue = (value) => {
    emit('update:modelValue', value);
  };

  watch(
    () => props.modelValue,
    (modelValue) => {
      if (modelValue) {
        getArea();
        if (props.data) {
          assignFields({
            ...props.data
          });
          isUpdate.value = true;
        } else {
          isUpdate.value = false;
        }
      } else {
        resetFields();
        formRef.value?.clearValidate?.();
      }
    }
  );
</script>
